package happyNumber;

import java.util.HashSet;
import java.util.Set;

public class HappyNumber {
    public static boolean isHappy(int n) {
        Set<Integer> repeat = new HashSet<>();
        int turn=0;
        while (n != 1 && !repeat.contains(n)&&turn<100000) {
            repeat.add(n);
            n = sumOfSquares(n);
            turn++;
        }
        return n == 1;
    }

    // Helper method to calculate the sum of squares of digits of a number
    private static int sumOfSquares(int number) {
        int sum = 0;
        while (number > 0) {
            int digit = number % 10;
            sum += digit * digit;
            number /= 10;
        }
        return sum;
    }
}
